package com.fzj.ssh.dao.impl;

import com.fzj.ssh.dao.UserDao;
import com.fzj.ssh.domain.SysUser;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.io.Serializable;

/**
 * Created by j on 2016/5/31.
 */
@Repository("UserDao")
public class UserDaoImpl implements UserDao{
    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
//    直接只使用@Autowired,这里提示sessionFactory没办法注入，必须再加上@Qualifier


    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public Session getCurrentSession(){
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public Serializable saveUser(SysUser user){
        return this.getCurrentSession().save(user);
    }

    @Override
    public SysUser getUser(String userId){
        return (SysUser) this.getCurrentSession().get(SysUser.class,userId);
    }
}
